Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming
Assigning object references
You can freely assign one object reference to another object reference if they are defined with compatible class types. The object references in an assignment have compatible class types if one of the following is true:
- Both references are defined as the same class or interface type (
rCustObj=rCustObjorrIBusObj=rIBusObj).- The left-hand reference is defined as a super class of the right-hand reference (
rCommonObj=rCustObj).- The left-hand reference is defined as an interface implemented by the right-hand reference (
rIBusObj=rCustObj).This assignment always sets the left-hand object reference to a copy of the right-hand object reference that points to the same object, not a reference to a new copy of the object.
Also, if the left-hand object reference already has a reference to an object, and there is no other available reference to that object, you must make sure to delete that object before reassigning the reference. Otherwise, you have created a memory leakāthe previous reference is overwritten with the new value and the previous object continues to exist with no object references available to delete it. This is very similar to the precautions you must take to avoid memory leaks while maintaining persistent procedures and other types of dynamic 4GL objects.
When you assign a subclass object reference to a super class object reference, the new object reference still points to the original object instance, but provides access to the object according to the super class type. Thus, if you use the super class object reference, you can only access the methods defined in that super class.
You can also assign an interface object reference to any class object reference, where the class implements the specified interface type at any level in its class hierarchy. If you use the interface object reference, you can only access the methods defined in the interface.
So an object reference can be copied to an object reference of the same type, a super type, or an interface that the object implements. The converse of this rule is not true. An object reference to a super class cannot be copied to a subclass object reference unless you use the
CASTfunction. The compiler rejects this assignment and only allows it if an appropriateCASTfunction is provided on the right-hand side (see the "Assignment and the CAST function" section).The following variation on the sample
Mainclass demonstrates compatible assignment between different types of object references using the sample classes. These assignments are indicated in thisMainclass code by the following numbered lines:This is the
Mainclass with the numbered lines:
Note that all of these object references, in fact, point to the same instance of one object. Thus, the destructor for
Maininvokes only oneDELETEOBJECTstatement for theacme.myObjs.CustObjinstance. When the destructor executes, this statement deletes the instance ofacme.myObjs.CustObjthat all three object references point to, and all four variables become invalid object references.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |